home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / datecls.exe / DATECLS4.H < prev    next >
C/C++ Source or Header  |  1993-03-07  |  6KB  |  166 lines

  1. /*
  2.  *┌──────────────────────────────────────────────────────────────────────
  3.  *│ File.........: DATECLS4.H
  4.  *│ Date.........: Sunday 3/7/1993
  5.  *│ Author.......: Kenneth A. Argo (CIS 71241,3635)
  6.  *│ Copyright....: None! Use freely.
  7.  *│ Version......: 4.1    Compile w/MSC++ 7.0
  8.  *│ Usage........: General purpose date conversion, arithmetic,
  9.  *│         :    comparison, and formatting class
  10.  *│ Compile line.: cl /AM /W3 /Zp /D_DOS /Osel /Gs /c datecl4.cpp
  11.  *│         : cl /AM /W3 /Zp /D_DOS /Osel /Gs /c datedemo.cpp
  12.  *│ Link line....:
  13.  *│    link /NOD /ONERROR:NOEXE datedemo date, datedemo, NUL, mafxcr mlibce;
  14.  *│
  15.  *│ Acknowledgements:
  16.  *│
  17.  *│    Originally inspired by Steve Marcus (CIS 72007,1233)  6/16/91
  18.  *│    Enhanced by Eric Simon (CIS 70540,1522)             6/29/91
  19.  *│    Further Enhanced by Chris Hill (CIS 72030,2606)         7/11/91
  20.  *│    Still Further Enhanced by Hill & Simon  v3.10         8/05/91
  21.  *│
  22.  *│    "It just keeps on a 'git 'n bedder!"
  23.  *│     by Charles D. Price (CIS 70541,3651) v4.0         6/27/92
  24.  *│
  25.  *│    Sealing the memory leaks...
  26.  *│    some variable casts and string output.
  27.  *│     by Kenneth A. Argo (CIS 71241,3635) v4.1         3/10/93
  28.  *│
  29.  *│    And the quest for the perfect date class continues....
  30.  *│
  31.  *└──────────────────────────────────────────────────────────────────────
  32.  */
  33. #ifndef __cplusplus
  34. #error  Requires C++ Compiler
  35. #endif
  36.  
  37. #ifndef DATECLS_H
  38. #define DATECLS_H
  39.  
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <iostream.h>
  44. #include <dos.h>
  45.  
  46. #define PUBLIC           // just a couple of friendly reminders!
  47. #define MEMBER
  48.  
  49. #define ABBR_LENGTH 3
  50.  
  51. const enum format_type {MDY, DAY, MONTH, FULL, EUROPEAN};
  52. const enum {OFF, ON};
  53.  
  54. const unsigned char NO_CENTURY  = 0x02;
  55. const unsigned char DATE_ABBR    = 0x04;
  56.  
  57. const char BadDate[] = "invalid date";    // so we don't repeat for every "INVALID" Date object !
  58.  
  59. class Date
  60. {
  61.     protected:
  62.         unsigned long julian;        // see julDate();  days since 1/1/4713 B.C.
  63.         unsigned int year;        // see NYear4()
  64.         unsigned char month;        // see NMonth()
  65.         unsigned char day;        // see Day()
  66.         unsigned char day_of_week;    // see NDOW();    1 = Sunday, ... 7 = Saturday
  67.         char *buf;            // easier to track memory leaks if we only have 1 pointer
  68.  
  69.     private:
  70.         static int DisplayFormat;
  71.         static unsigned char DisplayOptions;
  72.  
  73.         void julian_to_mdy ();           // convert julian day to mdy
  74.         void julian_to_wday ();        // convert julian day to day_of_week
  75.         void mdy_to_julian ();           // convert mdy to julian day
  76.  
  77.     public:
  78.         Date ();
  79.         Date (const long j);
  80.         Date (const int m, const int d, const int y);
  81.         Date (char *dat);
  82.         Date (const _dosdate_t &ds);
  83.         Date (const Date &dt);
  84.         ~Date();
  85.  
  86.         operator char * (void);
  87.  
  88.         Date &operator + (const long i);
  89.         Date &operator +  (const int  i);
  90.  
  91.         Date &operator - (const long i);
  92.         Date &operator - (const int  i);
  93.         long  operator - (const Date &dt);
  94.  
  95.         Date &operator += (const long i);
  96.         Date &operator -= (const long i);
  97.  
  98.         Date &operator ++ ();      // Prefix increment
  99.         Date &operator ++ (int);  // Postfix increment
  100.         Date &operator -- ();      // Prefix decrement
  101.         Date &operator -- (int);  // Postfix decrement
  102.  
  103.         friend int operator <  (const Date &dt1, const Date &dt2);
  104.         friend int operator <= (const Date &dt1, const Date &dt2);
  105.         friend int operator >  (const Date &dt1, const Date &dt2);
  106.         friend int operator >= (const Date &dt1, const Date &dt2);
  107.         friend int operator == (const Date &dt1, const Date &dt2);
  108.         friend int operator != (const Date &dt1, const Date &dt2);
  109.  
  110.         friend ostream &operator << (ostream &os, const Date &dt);
  111.         friend ostream &operator << (ostream &os, const _dosdate_t &dt);
  112.  
  113.         char *formatDate(const int type=DisplayFormat) const;
  114.         static void  setFormat (const int format);
  115.         static int   setOption (const int option, const int action=ON);
  116.  
  117.         long  julDate()     const;    // returns julian date
  118.         int   DOY()        const;    // returns relative date since Jan. 1
  119.         int   isLeapYear()    const;    // returns 1 if leap year, 0 if not
  120.  
  121.         // note that the next functions return a date struct as defined in
  122.         // dos.h (distinct from the Date class)
  123.  
  124.         _dosdate_t  eom()    const;    // returns last day of month in object
  125.         _dosdate_t  getDate()    const;    // returns a date structure
  126.  
  127.  
  128.         //─────────────────────────────────────────────────
  129.         // Version 4.0 Extension to Public Interface - CDP
  130.         //─────────────────────────────────────────────────
  131.  
  132.         // Theses 'Set's modify the date object and actually SET it
  133.         // They all return a reference to self (*this)
  134.  
  135.         Date &Set();        // Sets to current system date
  136.         Date &Set(long lJulian);
  137.         Date &Set(int nMonth, int nDay, int nYear);
  138.  
  139.         Date &AddWeeks(int nCount = 1);  //
  140.         Date &AddMonths(int nCount = 1); // May also pass neg# to decrement
  141.         Date &AddYears(int nCount = 1);  //
  142.  
  143.         int Day() const;    // Numeric Day of date object
  144.  
  145.         int DaysInMonth();    // Number of days in month (1..31)
  146.  
  147.         int FirstDOM() const; // First Day Of Month  (1..7)
  148.  
  149.         char *CDOW();        // Character Day Of Week ('Sunday'..'Saturday')
  150.         int NDOW() const;    // (1..7)
  151.  
  152.         int WOM();        // Numeric Week Of Month  (1..6)
  153.         int WOY();        // Numeric Week Of Year   (1..52)
  154.  
  155.         char *CMonth();    // Character Month name
  156.         int  NMonth() const;    // Month Number (1..12)
  157.         Date BOM();        // First Date Of Month
  158.         Date EOM();        // Last Date Of Month
  159.  
  160.         int  NYear4() const;    // eg. 1992
  161.         Date BOY();        // First Date Of Year
  162.         Date EOY();        // Last Date Of Year
  163. };
  164.  
  165. #endif
  166.